<?php

namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use app\common\model\tang\KwhLog;
use app\common\model\tang\KwhWaterLog;
use app\common\model\tang\MerchantMoney;
use app\common\model\tang\MerchantMonthMoney as EmModel;
use app\common\model\tang\MonthSoleElectricityLog;
use app\common\model\tang\MonthWaterLog;


class MerchantMonthMoney extends AdminBaseController
{

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new EmModel();
    }

    public function index()
    {
        if (IS_AJAX) {
            return $this->model->getAdminPageData($this->param);
        }
        $merchant_list = getMerchant();
        //获取菜单列表
        $this->assign(compact("merchant_list"));
        return $this->fetch();
    }

    public function detail(){
        $id = $this->request->param("id/d");//父id
        $month = $this->request->param("dataTime");//父id
        $monthLast = date('Y-m', strtotime($month . " - 2 month"));
        $money = (new MerchantMoney())->where('dateTime', $monthLast)->find();
        //获取本月开始值和结束值
        $tang_kwh_log = new KwhLog();
        $tang_kwh_water_log = new KwhWaterLog();
        //费率用电
        $Elepoint = $tang_kwh_log->where(['dataTime'=>$month,'merchant_id'=>$id])->distinct(true)->column('pointId');
        //单一用电
        $danPoint = $tang_kwh_water_log->where(['dataTime'=>$month,'merchant_id'=>$id,'type'=>1])->distinct(true)->column('pointId');
        //单一用水
        $waterpoint = $tang_kwh_water_log->where(['dataTime'=>$month,'merchant_id'=>$id,'type'=>2])->distinct(true)->column('pointId');
        $EleList = [];
        if(!empty($Elepoint)){
            foreach ($Elepoint as $k=>$v){
                $ElepointStart = $tang_kwh_log->where(['dataTime'=>$month,'merchant_id'=>$id,'pointId'=>$v])->order('datelog asc')->find();
                $ElepointEnd = $tang_kwh_log->where(['dataTime'=>$month,'merchant_id'=>$id,'pointId'=>$v])->order('datelog desc')->find();
                $data['fstart'] =$ElepointStart['fengStart'];
                $data['gstart'] =$ElepointStart['guStart'];
                $data['pstart'] =$ElepointStart['pingStart'];
                $data['fend'] =$ElepointEnd['fengEnd'];
                $data['gend'] =$ElepointEnd['guEnd'];
                $data['pend'] =$ElepointEnd['pingEnd'];
                $data['pointId'] =$v;
                $data['fpower'] =bcmul( bcsub($ElepointEnd['fengEnd'],$ElepointStart['fengStart'],2),$ElepointStart['rate'],2);
                $data['gpower'] =bcmul( bcsub($ElepointEnd['guEnd'],$ElepointStart['guStart'],2),$ElepointStart['rate'],2);
                $data['ppower'] =bcmul( bcsub($ElepointEnd['pingEnd'],$ElepointStart['pingStart'],2),$ElepointStart['rate'],2);
                if($ElepointStart['use_type']==6){
                    $money['tmoney'] =  $money['stmoney'];
                }
                $data['rate'] = $ElepointStart['rate'];
                $data['fmoney'] =  $money['fmoney'];
                $data['gmoney'] =  $money['gmoney'];
                $data['pmoney'] =  $money['pmoney'];
                $data['tmoney'] =  $money['tmoney'];
                $EleList[]=$data;
            }
        }
        $danList = [];
        if(!empty($danPoint)){
            foreach ($danPoint as $k=>$v){
                $danprice = (new MonthSoleElectricityLog())->order('dataTime desc')->where(['merchant_id'=>$id,'status'=>1])->find();
                if(!empty($danprice)){
                    $money['linmoney'] = $danprice['price'];
                }
                $ElepointStart = $tang_kwh_water_log->where(['dataTime'=>$month,'merchant_id'=>$id,'pointId'=>$v])->order('datelog asc')->find();

                $ElepointEnd = $tang_kwh_water_log->where(['dataTime'=>$month,'merchant_id'=>$id,'pointId'=>$v])->order('datelog desc')->find();
                if($ElepointStart){
                    $data['pointId'] =$v;
                    $data['money'] = $money['linmoney'];
                    $data['start'] = $ElepointStart['start'];
                    $data['end'] = $ElepointEnd['end'];
                    $data['rate'] =$danprice['rate'];
                    $data['power'] = bcmul($danprice['rate'],bcsub($ElepointEnd['end'],$ElepointStart['start'],2),2);
                    $danList[] =$data;
                }
            }
        }
        $waterLsit = [];
        if(!empty($waterpoint)){
            foreach ($waterpoint as $k=>$v){
                $danprice = (new MonthWaterLog())->order('dataTime desc')->where(['merchant_id'=>$id,'status'=>1])->value('price');
                if(!empty($danprice)){
                    $money['price'] = $danprice;
                }
                $ElepointStart = $tang_kwh_water_log->where(['dataTime'=>$month,'merchant_id'=>$id,'pointId'=>$v])->order('datelog asc')->find();
                $ElepointEnd = $tang_kwh_water_log->where(['dataTime'=>$month,'merchant_id'=>$id,'pointId'=>$v])->order('datelog desc')->find();
                if(!empty($ElepointStart)){
                    $data['pointId'] =$v;
                    $data['money'] =  $money['price'] ;
                    $data['start'] = $ElepointStart['start'];
                    $data['end'] = $ElepointEnd['end'];
                    $data['power'] = bcsub($ElepointEnd['end'],$ElepointStart['start'],2);
                    $waterLsit[] =$data;
                }

            }
        }
        $res['EleList'] = $EleList;
        $res['danList'] = $danList;
        $res['waterList'] = $waterLsit;
        $this->assign('list',$res);
        return $this->fetch();
    }
}